package com.baize.cpdepus.controller;

import com.baize.cpdepus.common.R;
import com.baize.cpdepus.entity.User;
import com.baize.cpdepus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/user")
@RestController
@Slf4j
@CrossOrigin  //跨域
public class UserController {
    @Autowired
    private UserService userService;

    //登入
    @RequestMapping("/login")
    public R login(@RequestBody User usr) {
        return R.success(userService.login(usr));
    }

    @RequestMapping("logout")
    public R logout() {
        userService.logout();
        return R.success("退出成功！");
    }

    @GetMapping("/userById")
    public R user(Integer id) {
        return R.success(userService.getById(id));
    }

    //注册
    @PostMapping("/register")
    public R register(User user, MultipartFile file) {
        userService.AddOne(user, file);
        return R.success("注册成功");

    }

    //修改密码
    @PutMapping("/updatePassword")
    public R updatePassword(@RequestBody User user) {
        userService.UpdatePasswrod(user);
        return R.success("修改成功");
    }

    //修改个性化信息
    @PutMapping("/update")
    public R update(@RequestBody User user, MultipartFile file) {
        userService.UpdateOne(user, file);
        return R.success("修改成功");
    }

    //注销账号
    @DeleteMapping
    public R delete(@RequestBody User user) {
        userService.Delete(user.getId());
        return R.success("删除成功");
    }

}
